<?php
// 显示所有错误
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// 引入必要文件
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/includes/functions.php';

// 初始化会话
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// 如果已经登录，直接跳转到控制台
if (isset($_SESSION['admin_user_id'])) {
    header('Location: index.php');
    exit;
}

// 创建PDO数据库连接
try {
    $dsn = "mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET;
    $pdo = new PDO($dsn, DB_USERNAME, DB_PASSWORD);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("数据库连接失败: " . $e->getMessage());
}

// 处理登录表单提交
$error = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    
    if (empty($username) || empty($password)) {
        $error = '请输入用户名和密码';
    } else {
        // 查询管理员
        $stmt = $pdo->prepare("SELECT * FROM " . DB_PREFIX . "users WHERE (username = ? OR email = ?) AND role = 'admin'");
        $stmt->execute([$username, $username]);
        $user = $stmt->fetch(PDO::FETCH_ASSOC);
        
        // 验证密码
        if ($user && password_verify($password, $user['password'])) {
            // 登录成功，设置会话
            $_SESSION['admin_user_id'] = $user['id'];
            $_SESSION['admin_username'] = $user['username'];
            
            // 更新最后登录时间
            $updateStmt = $pdo->prepare("UPDATE " . DB_PREFIX . "users SET last_login = NOW() WHERE id = ?");
            $updateStmt->execute([$user['id']]);
            
            // 设置闪存消息
            $_SESSION['flash_message'] = '登录成功！';
            $_SESSION['flash_type'] = 'success';
            
            // 重定向到控制台
            header('Location: index.php');
            exit;
        } else {
            $error = '用户名或密码错误';
        }
    }
}

// If already logged in, redirect to dashboard
if (isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true) {
    header('Location: dashboard.php');
    exit;
}

// Check for login error
$error_message = '';
if (isset($_SESSION['login_error'])) {
    $error_message = $_SESSION['login_error'];
    unset($_SESSION['login_error']);
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>KidsMind 管理员登录</title>
    <link rel="stylesheet" href="../css/bootstrap.min.css">
    <style>
        body {
            background: linear-gradient(135deg, #4158D0, #C850C0, #FFCC70);
            background-size: 400% 400%;
            animation: gradient 15s ease infinite;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        @keyframes gradient {
            0% { background-position: 0% 50%; }
            50% { background-position: 100% 50%; }
            100% { background-position: 0% 50%; }
        }
        
        .login-container {
            background-color: rgba(255, 255, 255, 0.9);
            border-radius: 10px;
            padding: 30px;
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
            width: 100%;
            max-width: 400px;
        }
        
        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }
        
        .login-header img {
            max-width: 80px;
            margin-bottom: 15px;
        }
        
        .login-header h1 {
            color: #333;
            font-size: 1.8rem;
            margin-bottom: 5px;
        }
        
        .login-header p {
            color: #666;
            font-size: 0.9rem;
        }
        
        .form-control {
            height: 50px;
            background-color: rgba(255, 255, 255, 0.8);
            border: 1px solid #ddd;
        }
        
        .btn-login {
            height: 50px;
            background: linear-gradient(to right, #4158D0, #C850C0);
            border: none;
            font-weight: bold;
        }
        
        .btn-login:hover {
            background: linear-gradient(to right, #3a4ec0, #b846af);
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }
        
        .error-message {
            color: #dc3545;
            font-size: 0.9rem;
            margin-top: 10px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-header">
            <img src="../images/logo.png" alt="KidsMind Logo">
            <h1>KidsMind 管理系统</h1>
            <p>请登录以访问内容管理功能</p>
        </div>
        
        <?php if (!empty($error_message)): ?>
        <div class="alert alert-danger" role="alert">
            <?php echo $error_message; ?>
        </div>
        <?php endif; ?>
        
        <form action="process_login.php" method="post">
            <div class="mb-3">
                <label for="username" class="form-label">用户名</label>
                <input type="text" class="form-control" id="username" name="username" required>
            </div>
            <div class="mb-3">
                <label for="password" class="form-label">密码</label>
                <input type="password" class="form-control" id="password" name="password" required>
            </div>
            <div class="d-grid gap-2">
                <button type="submit" class="btn btn-primary btn-login">登录</button>
            </div>
            <div class="text-center mt-3">
                <a href="../index.html" class="text-decoration-none">返回网站首页</a>
            </div>
        </form>
    </div>
    
    <script src="../js/bootstrap.bundle.min.js"></script>
</body>
</html> 